﻿// Decompiled with JetBrains decompiler
// Type: UnityEngine.Rendering.AttachmentDescriptor
// Assembly: UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E6AE6B5A-2752-4B07-9528-C8B77C62CE79
// Assembly location: C:\Users\windys\Unity\2021.3.39f1c1\Editor\Data\Managed\UnityEngine\UnityEngine.CoreModule.dll
// XML documentation location: C:\Users\windys\Unity\2021.3.39f1c1\Editor\Data\Managed\UnityEngine\UnityEngine.CoreModule.xml

using System;
using UnityEngine.Experimental.Rendering;

#nullable disable
namespace UnityEngine.Rendering
{
  /// <summary>
  ///   <para>A declaration of a single color or depth rendering surface to be attached into a RenderPass.</para>
  /// </summary>
  public struct AttachmentDescriptor : IEquatable<AttachmentDescriptor>
  {
    private RenderBufferLoadAction m_LoadAction;
    private RenderBufferStoreAction m_StoreAction;
    private GraphicsFormat m_Format;
    private RenderTargetIdentifier m_LoadStoreTarget;
    private RenderTargetIdentifier m_ResolveTarget;
    private Color m_ClearColor;
    private float m_ClearDepth;
    private uint m_ClearStencil;

    /// <summary>
    ///   <para>The load action to be used on this attachment when the RenderPass starts.</para>
    /// </summary>
    public RenderBufferLoadAction loadAction
    {
      get => this.m_LoadAction;
      set => this.m_LoadAction = value;
    }

    /// <summary>
    ///   <para>The store action to use with this attachment when the RenderPass ends. Only used when either ConfigureTarget or ConfigureResolveTarget has been called.</para>
    /// </summary>
    public RenderBufferStoreAction storeAction
    {
      get => this.m_StoreAction;
      set => this.m_StoreAction = value;
    }

    /// <summary>
    ///   <para>The GraphicsFormat of this attachment. To use in place of format.</para>
    /// </summary>
    public GraphicsFormat graphicsFormat
    {
      get => this.m_Format;
      set => this.m_Format = value;
    }

    /// <summary>
    ///   <para>The format of this attachment.</para>
    /// </summary>
    public RenderTextureFormat format
    {
      get
      {
        return (GraphicsFormatUtility.IsDepthFormat(this.m_Format) || GraphicsFormatUtility.IsStencilFormat(this.m_Format)) && this.m_Format != GraphicsFormat.ShadowAuto ? RenderTextureFormat.Depth : GraphicsFormatUtility.GetRenderTextureFormat(this.m_Format);
      }
      set
      {
        this.m_Format = GraphicsFormatUtility.GetGraphicsFormat(value, RenderTextureReadWrite.Default);
      }
    }

    /// <summary>
    ///   <para>The surface to use as the backing storage for this AttachmentDescriptor.</para>
    /// </summary>
    public RenderTargetIdentifier loadStoreTarget
    {
      get => this.m_LoadStoreTarget;
      set => this.m_LoadStoreTarget = value;
    }

    /// <summary>
    ///   <para>When the renderpass that uses this attachment ends, resolve the MSAA surface into the given target.</para>
    /// </summary>
    public RenderTargetIdentifier resolveTarget
    {
      get => this.m_ResolveTarget;
      set => this.m_ResolveTarget = value;
    }

    /// <summary>
    ///   <para>The currently assigned clear color for this attachment. Default is black.</para>
    /// </summary>
    public Color clearColor
    {
      get => this.m_ClearColor;
      set => this.m_ClearColor = value;
    }

    /// <summary>
    ///   <para>Currently assigned depth clear value for this attachment. Default value is 1.0.</para>
    /// </summary>
    public float clearDepth
    {
      get => this.m_ClearDepth;
      set => this.m_ClearDepth = value;
    }

    /// <summary>
    ///   <para>Currently assigned stencil clear value for this attachment. Default is 0.</para>
    /// </summary>
    public uint clearStencil
    {
      get => this.m_ClearStencil;
      set => this.m_ClearStencil = value;
    }

    /// <summary>
    ///   <para>Binds this AttachmentDescriptor to the given target surface.</para>
    /// </summary>
    /// <param name="tgt">The surface to use as the backing storage for this AttachmentDescriptor.</param>
    /// <param name="loadExistingContents">Whether to read in the existing contents of the surface when the RenderPass starts.</param>
    /// <param name="storeResults">Whether to store the rendering results of the attachment when the RenderPass ends.</param>
    /// <param name="target"></param>
    public void ConfigureTarget(
      RenderTargetIdentifier target,
      bool loadExistingContents,
      bool storeResults)
    {
      this.m_LoadStoreTarget = target;
      if (loadExistingContents && this.m_LoadAction != RenderBufferLoadAction.Clear)
        this.m_LoadAction = RenderBufferLoadAction.Load;
      if (!storeResults)
        return;
      this.m_StoreAction = this.m_StoreAction != RenderBufferStoreAction.StoreAndResolve && this.m_StoreAction != RenderBufferStoreAction.Resolve ? RenderBufferStoreAction.Store : RenderBufferStoreAction.StoreAndResolve;
    }

    /// <summary>
    ///   <para>When the renderpass that uses this attachment ends, resolve the MSAA surface into the given target.</para>
    /// </summary>
    /// <param name="tgt">The target surface to receive the MSAA-resolved pixels.</param>
    /// <param name="target"></param>
    public void ConfigureResolveTarget(RenderTargetIdentifier target)
    {
      this.m_ResolveTarget = target;
      if (this.m_StoreAction == RenderBufferStoreAction.StoreAndResolve || this.m_StoreAction == RenderBufferStoreAction.Store)
        this.m_StoreAction = RenderBufferStoreAction.StoreAndResolve;
      else
        this.m_StoreAction = RenderBufferStoreAction.Resolve;
    }

    /// <summary>
    ///   <para>When the RenderPass starts, clear this attachment into the color or depth/stencil values given (depending on the format of this attachment). Changes loadAction to RenderBufferLoadAction.Clear.</para>
    /// </summary>
    /// <param name="clearCol">Color clear value. Ignored on depth/stencil attachments.</param>
    /// <param name="clearDep">Depth clear value. Ignored on color surfaces.</param>
    /// <param name="clearStenc">Stencil clear value. Ignored on color or depth-only surfaces.</param>
    /// <param name="clearColor"></param>
    /// <param name="clearDepth"></param>
    /// <param name="clearStencil"></param>
    public void ConfigureClear(Color clearColor, float clearDepth = 1f, uint clearStencil = 0)
    {
      this.m_ClearColor = clearColor;
      this.m_ClearDepth = clearDepth;
      this.m_ClearStencil = clearStencil;
      this.m_LoadAction = RenderBufferLoadAction.Clear;
    }

    /// <summary>
    ///   <para>Create a AttachmentDescriptor to be used with RenderPass.</para>
    /// </summary>
    /// <param name="fmt">The format of this attachment.</param>
    /// <param name="format"></param>
    /// <param name="target"></param>
    /// <param name="loadExistingContents"></param>
    /// <param name="storeResults"></param>
    /// <param name="resolve"></param>
    public AttachmentDescriptor(GraphicsFormat format)
      : this()
    {
      this.m_LoadAction = RenderBufferLoadAction.DontCare;
      this.m_StoreAction = RenderBufferStoreAction.DontCare;
      this.m_Format = format;
      this.m_LoadStoreTarget = new RenderTargetIdentifier(BuiltinRenderTextureType.None);
      this.m_ResolveTarget = new RenderTargetIdentifier(BuiltinRenderTextureType.None);
      this.m_ClearColor = new Color(0.0f, 0.0f, 0.0f, 0.0f);
      this.m_ClearDepth = 1f;
    }

    /// <summary>
    ///   <para>Create a AttachmentDescriptor to be used with RenderPass.</para>
    /// </summary>
    /// <param name="fmt">The format of this attachment.</param>
    /// <param name="format"></param>
    /// <param name="target"></param>
    /// <param name="loadExistingContents"></param>
    /// <param name="storeResults"></param>
    /// <param name="resolve"></param>
    public AttachmentDescriptor(RenderTextureFormat format)
      : this(GraphicsFormatUtility.GetGraphicsFormat(format, RenderTextureReadWrite.Default))
    {
    }

    /// <summary>
    ///   <para>Create a AttachmentDescriptor to be used with RenderPass.</para>
    /// </summary>
    /// <param name="fmt">The format of this attachment.</param>
    /// <param name="format"></param>
    /// <param name="target"></param>
    /// <param name="loadExistingContents"></param>
    /// <param name="storeResults"></param>
    /// <param name="resolve"></param>
    public AttachmentDescriptor(
      RenderTextureFormat format,
      RenderTargetIdentifier target,
      bool loadExistingContents = false,
      bool storeResults = false,
      bool resolve = false)
      : this(GraphicsFormatUtility.GetGraphicsFormat(format, RenderTextureReadWrite.Default))
    {
    }

    public bool Equals(AttachmentDescriptor other)
    {
      return this.m_LoadAction == other.m_LoadAction && this.m_StoreAction == other.m_StoreAction && this.m_Format == other.m_Format && this.m_LoadStoreTarget.Equals(other.m_LoadStoreTarget) && this.m_ResolveTarget.Equals(other.m_ResolveTarget) && this.m_ClearColor.Equals(other.m_ClearColor) && this.m_ClearDepth.Equals(other.m_ClearDepth) && (int) this.m_ClearStencil == (int) other.m_ClearStencil;
    }

    public override bool Equals(object obj)
    {
      return obj != null && obj is AttachmentDescriptor other && this.Equals(other);
    }

    public override int GetHashCode()
    {
      return (((((int) ((GraphicsFormat) ((int) ((RenderBufferStoreAction) ((int) this.m_LoadAction * 397) ^ this.m_StoreAction) * 397) ^ this.m_Format) * 397 ^ this.m_LoadStoreTarget.GetHashCode()) * 397 ^ this.m_ResolveTarget.GetHashCode()) * 397 ^ this.m_ClearColor.GetHashCode()) * 397 ^ this.m_ClearDepth.GetHashCode()) * 397 ^ (int) this.m_ClearStencil;
    }

    public static bool operator ==(AttachmentDescriptor left, AttachmentDescriptor right)
    {
      return left.Equals(right);
    }

    public static bool operator !=(AttachmentDescriptor left, AttachmentDescriptor right)
    {
      return !left.Equals(right);
    }
  }
}
